package com.sh.data.engine.web.webapi.controller.normalization;

import com.sh.data.engine.application.model.dto.base.PagerResponse;
import com.sh.data.engine.application.model.dto.normalization.standard.*;
import com.sh.data.engine.application.model.request.normalization.*;
import com.sh.data.engine.application.service.normalization.DictionaryApplicationService;
import com.sh.data.engine.common.annotation.fuzzyQuery.FuzzyMethod;
import com.sh.data.engine.common.util.excel.EasyExcelUtil;
import com.sh.data.engine.domain.annotation.LogExecution;
import com.sh.data.engine.domain.annotation.userlog.Business;
import com.sh.data.engine.domain.annotation.userlog.EngineOperation;
import com.sh.data.engine.web.webapi.controller.base.response.RestResponseEntity;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

/**
 * @author jingchen.hr
 */
@Tag(name = "规范设计-数据标准", description = "规范设计-数据标准API")
@RequestMapping("/dictionary")
@RestController
@Slf4j
@Validated
public class DictionaryController {

    @Autowired
    private DictionaryApplicationService dictionaryApplicationService;

    //    @AuthorityAnnotation(menuId = MenuEnumNew.CCGL2, readWriteStatus = 0, isAdd = 1)
    @Operation(summary = "数据标准常量列表", description = "数据标准常量列表")
    @PostMapping("/cons/list")
    public RestResponseEntity<DictionaryConsResponse> consList() {
        var result = dictionaryApplicationService.consList();
        return RestResponseEntity.success(result);
    }

    /**
     * 新增字典
     *
     * @param request
     * @return
     */
//    @AuthorityAnnotation(menuId = MenuEnumNew.BZDY, readWriteStatus = 1, isAdd = 1)
    @Operation(summary = "添加标准", description = "添加标准")
    @PostMapping("/add")
    @LogExecution(operation = EngineOperation.ADD, business = Business.数据标准)
//    @Log(
//        business = Business.数据标准,
//        operation = Operation.ADD)
    public RestResponseEntity<Boolean> addDictionary(@Valid @RequestBody DictionaryRequest request) {
        var result = dictionaryApplicationService.addDictionary(request);
        return RestResponseEntity.success(result);
    }

    /**
     * 获取部门
     *
     * @return
     */
//    @AuthorityAnnotation(menuId = MenuEnumNew.BZDY, readWriteStatus = 0, isAdd = 1)
//    @Operation(summary = "获取所有部门", description = "获取部门")
//    @GetMapping("/getDataOwnerList")
//    public RestResponseEntity<List<DepartmentResponse>> getAllDataOwners() {
//
//        Long projectId = ServletUtils.getProjectId();
//        List<DataOwnerDomain> allByProjectId = dataOwnerService.getAllByProjectId(projectId);
//        List<DepartmentResponse> departmentResponses =
//            ConvertUtil.copyProperties(allByProjectId, DepartmentResponse.class);
//        return new RestResponseEntity<>(departmentResponses);
//    }

    /**
     * 获取部门
     *
     * @return
     */
//    @AuthorityAnnotation(menuId = MenuEnumNew.BZDY, readWriteStatus = 0, isAdd = 1)
    @Operation(summary = "获取主题域（目录结构）", description = "获取主题域（目录结构）")
    @GetMapping("/getThemeList")
    public RestResponseEntity<List<DictionaryGroupResponse>> getThemesList() {
        var result = dictionaryApplicationService.getThemesList();
        return RestResponseEntity.success(result);
    }

    /**
     * 编辑字典
     *
     * @param request
     * @return
     */
//    @AuthorityAnnotation(menuId = MenuEnumNew.BZDY, readWriteStatus = 1, isAdd = 1)
    @Operation(summary = "编辑标准", description = "编辑标准")
    @PostMapping("/edit")
    @LogExecution(operation = EngineOperation.UPDATE, business = Business.数据标准)
//    @Log(
//        business = Business.数据标准,
//        operation = Operation.UPDATE)
    public RestResponseEntity<Boolean> editDictionary(@Valid @RequestBody DictionaryRequest request) {
        var result = dictionaryApplicationService.editDictionary(request);
        return RestResponseEntity.success(result);
    }

    //    @AuthorityAnnotation(menuId = MenuEnumNew.BZDY, readWriteStatus = 1, isAdd = 1)
    @Operation(summary = "标准管理列表", description = "标准管理列表")
    @FuzzyMethod
    @GetMapping("/list")
    public RestResponseEntity<PagerResponse<DictionaryResponse>> listDictionary(
        @Validated DictionaryQueryRequest request) {
        var result = dictionaryApplicationService.listDictionary(request);
        return RestResponseEntity.success(result);
    }

    //    @BizDataIndexAnnotation(
//        type = BizDataIndexTypeEnum.STANDARD,
//        dataIdFieldName = "id",
//        dataIdParamIndex = 0,
//        operateType = BizDataIndexAnnotation.OperateType.DELETE)
//    @AuthorityAnnotation(menuId = MenuEnumNew.BZDY, readWriteStatus = 1, isAdd = 1)
    @Operation(summary = "删除标准", description = "删除标准")
    @PostMapping("/delete")
    @LogExecution(operation = EngineOperation.DEL, business = Business.数据标准)
//    @Log(
//        business = Business.数据标准,
//        operation = Operation.DEL)
    public RestResponseEntity<Boolean> deleteDictionary(
        @Valid @RequestBody DictionaryOptRequest request) {
        var result = dictionaryApplicationService.deleteDictionary(request);
        return RestResponseEntity.success(result);
    }

    //    @BizDataIndexAnnotation(
//        type = BizDataIndexTypeEnum.STANDARD,
//        dataIdFieldName = "id",
//        dataIdParamIndex = 0,
//        operateType = BizDataIndexAnnotation.OperateType.SAVE)
//    @AuthorityAnnotation(menuId = MenuEnumNew.BZDY, readWriteStatus = 1, isAdd = 1)
    @Operation(summary = "下线或发布标准", description = "下线或发布标准")
    @PostMapping("/release")
    @LogExecution(operation = EngineOperation.DEPUBLISH, business = Business.数据标准)
//    @Log(
//        business = Business.数据标准,
//        operation = Operation.DEPUBLISH)
    public RestResponseEntity<Boolean> releaseDictionary(
        @Valid @RequestBody DictionaryOptRequest request) {
        var result = dictionaryApplicationService.releaseDictionary(request);
        return RestResponseEntity.success(result);
    }

    //    @AuthorityAnnotation(menuId = MenuEnumNew.BZDY, readWriteStatus = 1, isAdd = 1)
    @Operation(summary = "标准详情查询", description = "标准详情查询")
    @GetMapping("/detail")
    public RestResponseEntity<DictionaryDetailResponse> detailDictionary(
        @Valid @ModelAttribute DictionaryOptRequest request) {
        var result = dictionaryApplicationService.detailDictionary(request);
        return RestResponseEntity.success(result);
    }

    //    @AuthorityAnnotation(menuId = MenuEnumNew.BZDY, readWriteStatus = 1, isAdd = 1)
    @Operation(summary = "数据标准导入模板下载", description = "数据标准导入模板下载")
    @GetMapping("/import/template/download")
    @LogExecution(operation = EngineOperation.DOWNLOAD, business = Business.数据标准)
//    @Log(
//        business = Business.数据标准,
//        operation = Operation.DOWNLOAD)
    public void importTemplateDownload(HttpServletResponse response) {
        EasyExcelUtil.importExcelDownload(response, "template/dictionary_template.xlsx", "数据标准模板");
    }

    //    @BizDataIndexAnnotation(
//        type = BizDataIndexTypeEnum.STANDARD,
//        dataIdFieldName = "id",
//        dataIdParamIndex = 0,
//        operateType = BizDataIndexAnnotation.OperateType.SAVE)
//    @AuthorityAnnotation(menuId = MenuEnumNew.BZDY, readWriteStatus = 1, isAdd = 1)
    @Operation(summary = "数据标准导入", description = "数据标准导入")
    @PostMapping(value = "/import/excel")
    @LogExecution(operation = EngineOperation.UPLOAD, business = Business.数据标准)
//    @Log(
//        business = Business.数据标准,
//        operation = Operation.UPLOAD)
    public RestResponseEntity<Boolean> importExcel(@RequestPart("file") MultipartFile file) {
        var result = dictionaryApplicationService.importExcel(file);
        return RestResponseEntity.success(result);
    }

    //    @BizDataIndexAnnotation(
//        type = BizDataIndexTypeEnum.STANDARD,
//        dataIdFieldName = "ids",
//        dataIdParamIndex = 0,
//        operateType = BizDataIndexAnnotation.OperateType.SAVE)
//    @AuthorityAnnotation(menuId = MenuEnumNew.BZDY, readWriteStatus = 1, isAdd = 1)
    @Operation(summary = "批量发布/批量删除/批量下线", description = "批量发布/批量删除/批量下线")
    @PostMapping("/batch/opt")
    public RestResponseEntity<Boolean> batchOpt(
        @Valid @RequestBody DictionaryBatchOptRequest request) {
        var result = dictionaryApplicationService.batchOpt(request);
        return RestResponseEntity.success(result);
    }

    //    @AuthorityAnnotation(menuId = MenuEnumNew.BZDY, readWriteStatus = 1, isAdd = 1)
    @Operation(summary = "数据标准导出", description = "数据标准导出")
    @GetMapping("/export/list")
    @LogExecution(operation = EngineOperation.EXPORT, business = Business.数据标准)
//    @Log(
//        business = Business.数据标准,
//        operation = Operation.EXPORT)
    public void exportList(
        HttpServletResponse response, @Validated @ModelAttribute DictionaryBatchOptRequest request) {
        dictionaryApplicationService.exportList(request, response);
    }

    //    @AuthorityAnnotation(menuId = MenuEnumNew.BZDY, readWriteStatus = 1, isAdd = 1)
    @Operation(summary = "获取字段的标准信息")
    @PostMapping("getDicInfoByFieldNames")
    public RestResponseEntity<GetDicInfoByFieldNamesResponse> getDicInfoByFieldNames(
        @RequestBody GetDicInfoByFieldNamesRequest request) {
        var result = dictionaryApplicationService.getDicInfoByFieldNames(request);
        return RestResponseEntity.success(result);
    }


}
